package com.cdut.drivingschool.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Scroller;

import com.cdut.drivingschool.R;
public class PushScrollView extends ScrollView {  
    private Context mContext;  
    private Scroller mScroller;  
      
    int left, top;  
    float startX, startY, currentX, currentY;  
    int bgViewH, iv1W;  
    int rootW, rootH;  
    View headView;  
    View bgView;  
    boolean scrollerType;  
    static final int len = 0xc8;  
  
    public PushScrollView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        this.mContext = context;  
        mScroller = new Scroller(mContext);  
    }  
  
    public PushScrollView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        this.mContext = context;  
        mScroller = new Scroller(mContext);  
    }  
  
    public PushScrollView(Context context) {  
        super(context);  
        this.mContext = context;  
        mScroller = new Scroller(mContext);  
    }  
    public void setHeaderView(View view)
    {
    	headView = view;
    }
    @Override   
    public boolean dispatchTouchEvent(MotionEvent event) {  
        int action = event.getAction();  
        if (!mScroller.isFinished()) {  
            return super.onTouchEvent(event);  
        }  
        bgView = headView.findViewById(R.id.bg_wall);  
        currentX = event.getX();  
        currentY = event.getY();  
          
        headView.getTop();  
        switch (action) {  
        case MotionEvent.ACTION_DOWN:  
              
            left = bgView.getLeft();  
            top = bgView.getBottom();  
            rootW = getWidth();  
            rootH = getHeight();  
            bgViewH = bgView.getHeight();  
            startX = currentX;  
            startY = currentY;  
  
            break;  
        case MotionEvent.ACTION_MOVE:  
          
            if (headView.isShown() && headView.getTop() >= 0) {  
  
                int t = getScrollY(currentY - startY);  
                  
                if (t >= top && t <= headView.getBottom() + len) {  
                    bgView.setLayoutParams(new RelativeLayout.LayoutParams(bgView.getWidth(), t));  
                }  
                scrollerType = false;  
            }  
            break;  
        case MotionEvent.ACTION_UP:  
              
            scrollerType = true;  
            mScroller.startScroll(bgView.getLeft(), bgView.getBottom(),  
                      
                    0 - bgView.getLeft(), bgViewH - bgView.getBottom(), 200);  
            invalidate();  
            break;  
        }  
  
        return super.dispatchTouchEvent(event);  
    }  
    private int getScrollY(float dy) {  
        int yy = (int) (top + dy/4.0F);  
        return yy;  
    }  
    public void computeScroll() {  
        //super.computeScroll();  
        if (mScroller.computeScrollOffset()) {  
            int x = mScroller.getCurrX();  
            int y = mScroller.getCurrY();  
          
            bgView.layout(0, 0, x + bgView.getWidth(), y);  
            invalidate();  
              
            if (!mScroller.isFinished() && scrollerType && y > 200) {  
                bgView.setLayoutParams(new RelativeLayout.LayoutParams(bgView.getWidth(), y));  
            }  
        }  
    }  
}  